package com.zhsm.fuxi.mvp.ui.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.zhsm.fuxi.R;
import com.zhsm.fuxi.base.BaseDialog;
import com.zhsm.fuxi.view.widget.PinEntryEditText;

/**
 * 验证码
 */

public class VerificationCodeDialog extends BaseDialog implements View.OnClickListener {
    TextView inputError;
    ImageView imageCode;
    PinEntryEditText txtPinEntry;
    ImageView refreshCode;
    private Context mContext;

    public VerificationCodeDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        inputError = findViewById(R.id.input_error);
        imageCode = findViewById(R.id.image_code);
        txtPinEntry = findViewById(R.id.txt_pin_entry);
        refreshCode = findViewById(R.id.refresh_code);
        refreshCode.setOnClickListener(this);
    }

    public VerificationCodeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_image_code;
    }

    @Override
    protected int getGravity() {
        return GRAVITY_CENTER;
    }

    @Override
    protected void initView() {
        txtPinEntry.setOnPinEnteredListener(str -> {
            if (!TextUtils.isEmpty(str) && verificationCodeDialogImp != null) {
                verificationCodeDialogImp.sendVerificationCode(str.toString());
            }
        });
        txtPinEntry.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    setinputError(false, null);
                }
            }
        });
    }

    @Override
    protected boolean isCanceledOnTouchOutside() {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.refresh_code:
                if (verificationCodeDialogImp != null)
                    verificationCodeDialogImp.refreshVerification();
                break;
            case R.id.txt_pin_entry:
                break;
        }
    }

    /**
     * 验证码错误提醒,错误内容由后台返回
     *
     * @param b
     */
    public void setinputError(boolean b, String error) {
        if (b) {
            if (!TextUtils.isEmpty(error)) {
                inputError.setText(error);
                inputError.setVisibility(View.VISIBLE);
            }
        } else {
            inputError.setVisibility(View.GONE);
        }
    }

    public ImageView getImageCode() {
        return imageCode;
    }

    public PinEntryEditText getPinEntry() {
        return txtPinEntry;
    }

    public void showImageCode(Bitmap bitmap) {
        if (!isShowing())
            show();
        imageCode.setImageBitmap(bitmap);
    }


    public interface VerificationCodeDialogImp {
        /**
         * 刷新验证码
         */
        void refreshVerification();

        /**
         * 发送验证码
         *
         * @param code
         */
        void sendVerificationCode(String code);
    }

    private VerificationCodeDialogImp verificationCodeDialogImp;

    public void setVerificationCodeLisenter(VerificationCodeDialogImp codeLisenter) {
        this.verificationCodeDialogImp = codeLisenter;
    }

}
